package data
{
	import kinds.OpcodeKind;
	
	import tag.doABC2.CPool;
	
	import util.BytesUtil;

	public class Opcode
	{
		/** 位置*/
		public var position:uint = 0;
		
		/** 标识*/
		public var flag:uint = 0;
		
		/** 操作名*/
		public var name:String = "";
		
		/** 操作数据*/
		public var opValue:Array = [];
		
		public function toString():String
		{
			var ret:String = name + "\t";
			var cp:CPool = App.cp;
			switch(flag)
			{
				case OpcodeKind.OP_debugfile:
				case OpcodeKind.OP_pushstring:
					ret += cp.cp_string[opValue[0]].replace(/\n/g,"\\n").replace(/\t/g,"\\t") + '"  //stringIndex = ' + opValue[0];
					break
				case OpcodeKind.OP_pushnamespace:
					ret += cp.cp_multiname[opValue[0]];
					break
				case OpcodeKind.OP_pushint:
					ret += cp.cp_int[opValue[0]];
					break
				case OpcodeKind.OP_pushuint:
					ret += cp.cp_uint[opValue[0]];
					break;
				case OpcodeKind.OP_pushdouble:
					ret += cp.cp_double[opValue[0]];
					break;
				case OpcodeKind.OP_getsuper: 
				case OpcodeKind.OP_setsuper: 
				case OpcodeKind.OP_getproperty: 
				case OpcodeKind.OP_initproperty: 
				case OpcodeKind.OP_setproperty: 
				case OpcodeKind.OP_getlex: 
				case OpcodeKind.OP_findpropstrict: 
				case OpcodeKind.OP_findproperty:
				case OpcodeKind.OP_finddef:
				case OpcodeKind.OP_deleteproperty: 
				case OpcodeKind.OP_istype: 
				case OpcodeKind.OP_coerce: 
				case OpcodeKind.OP_astype: 
				case OpcodeKind.OP_getdescendants:
					ret += cp.cp_multiname[opValue[0]] + " //nameIndex = " + opValue[0];
					break;
				case OpcodeKind.OP_constructprop:
				case OpcodeKind.OP_callproperty:
				case OpcodeKind.OP_callproplex:
				case OpcodeKind.OP_callsuper:
				case OpcodeKind.OP_callsupervoid:
				case OpcodeKind.OP_callpropvoid:
					ret += cp.cp_multiname[opValue[0]] + "("+opValue[1]+")" + "//nameIndex = " + opValue[0];
					break;
				case OpcodeKind.OP_newfunction: 
					ret += App.abcFile.classArr[opValue[0]];
					break;
				
				case OpcodeKind.OP_callstatic:
					opValue.push(App.abcFile.methodArr[opValue[0]]) + "("+opValue[1]+")";
					break;
				case OpcodeKind.OP_newclass: 
					opValue.push(App.abcFile.classArr[opValue[0]]);
					break;
				case OpcodeKind.OP_lookupswitch:
					
					break;
				case OpcodeKind.OP_jump:
				case OpcodeKind.OP_iftrue:     case OpcodeKind.OP_iffalse:
				case OpcodeKind.OP_ifeq:       case OpcodeKind.OP_ifne:
				case OpcodeKind.OP_ifge:       case OpcodeKind.OP_ifnge:
				case OpcodeKind.OP_ifgt:       case OpcodeKind.OP_ifngt:
				case OpcodeKind.OP_ifle:       case OpcodeKind.OP_ifnle:
				case OpcodeKind.OP_iflt:       case OpcodeKind.OP_ifnlt:
				case OpcodeKind.OP_ifstricteq: case OpcodeKind.OP_ifstrictne:
					
					break;
				case OpcodeKind.OP_inclocal:
				case OpcodeKind.OP_declocal:
				case OpcodeKind.OP_inclocal_i:
				case OpcodeKind.OP_declocal_i:
				case OpcodeKind.OP_getlocal:
				case OpcodeKind.OP_kill:
				case OpcodeKind.OP_setlocal:
				case OpcodeKind.OP_debugline:
				case OpcodeKind.OP_getglobalslot:
				case OpcodeKind.OP_getslot:
				case OpcodeKind.OP_setglobalslot:
				case OpcodeKind.OP_setslot:
				case OpcodeKind.OP_pushshort:
				case OpcodeKind.OP_newcatch:
					ret += opValue[0];
					break
				case OpcodeKind.OP_debug:
					ret += opValue[0] + " " + opValue[1] + " " + opValue[2] + " " + opValue[3];
					break;
				case OpcodeKind.OP_newobject:
					ret += "{" + opValue[0] + "}";
					break;
				case OpcodeKind.OP_newarray:
					ret += "{" + opValue[0] + "}";
					break;
				case OpcodeKind.OP_call:
				case OpcodeKind.OP_construct:
				case OpcodeKind.OP_constructsuper:
				case OpcodeKind.OP_applytype:
					ret += "(" + opValue[0] + ")";
					break;
				case OpcodeKind.OP_pushbyte:
				case OpcodeKind.OP_getscopeobject:
					ret += opValue[0];
					break;
				case OpcodeKind.OP_hasnext2:
					ret += opValue[0] + opValue[1];
				default:
					/*if (opNames[opcode] == ("0x"+toString(16).toUpperCase()))
					s += " UNKNOWN OPCODE"*/
					break
			}
			return ret;
		}
	}
}